﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NCKH.Admin
{
    public partial class QLSV : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hiendonvi();
                hiennhomthuchien();
            }
        }
        private void hiendonvi()
        {
            string sqlSelect = "SELECT MaDV,TenDV FROM DONVI ";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cnn.Open();
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddrtendonvi.Items.Insert(0, "-- Chọn đơn vị --");
                        while (Rd.Read())
                        {
                            ddrtendonvi.Items.Add(
                                new ListItem(Rd["TenDV"].ToString(),
                                    Rd["MaDV"].ToString()));
                        }
                    }
                    Cnn.Close();
                }
            }
        }
        private void hiennhomthuchien()
        {
            string sqlSelect = "SELECT Manhom,Tennhom FROM NHOMTHUCHIEN ";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cnn.Open();
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddrnth.Items.Insert(0, "-- Chọn Nhóm thực hiện --");
                        while (Rd.Read())
                        {
                            ddrnth.Items.Add(
                                new ListItem(Rd["Tennhom"].ToString(),
                                    Rd["Manhom"].ToString()));
                        }
                    }
                    Cnn.Close();
                }
            }
        }
        private void timkiemSVtheoNTH()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("TKSVtheotennhom", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Manhom", ddrnth.SelectedValue);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        da.SelectCommand = Cmd;
                        DataTable tb = new DataTable("NHOMTHUCHIEN");
                        da.Fill(tb);
                        grvTTSV.DataSource = tb;
                        grvTTSV.DataBind();
                    }
                }
            }
        }


        private void timkiemSVtheotenDV()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("TKSVtheoTenDV", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaDV", ddrtendonvi.SelectedValue);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        da.SelectCommand = Cmd;
                        DataTable tb = new DataTable("DONVI");
                        da.Fill(tb);
                        grvTTSV.DataSource = tb;
                        grvTTSV.DataBind();
                    }
                }
            }
        }
        private void thongbao(string Noidung)
        {

            ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateCom", "alert('" + Noidung + "');return false;", true);
        }

        protected void btntimkiem_Click(object sender, EventArgs e)
        {
           timkiemSVtheoNTH();
            timkiemSVtheoNTH();
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            ddrnth.SelectedItem.Text = ddrtendonvi.SelectedItem.Text = string.Empty;
        }
    }
}